package com.glory.eapequipmentmonitor.adapter;

import android.support.annotation.Nullable;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.eapequipmentmonitor.R;
import com.glory.eapequipmentmonitor.bean.PackBean;

import java.util.List;


public class PackListAdapter extends BaseQuickAdapter<PackBean, BaseViewHolder> {
    private SparseBooleanArray mCheckStates = new SparseBooleanArray();

    public PackListAdapter(@Nullable List<PackBean> data) {
        super(R.layout.adapter_pack_list, data);
    }

    public void clearPos() {
        mCheckStates = new SparseBooleanArray();
    }

    public void setAll(boolean check) {
        for (int i = 0; i < getData().size(); i++) {
            mCheckStates.put(i, check);
            getData().get(i).setSelect(check);
        }
        notifyDataSetChanged();
    }

    @Override
    protected void convert(BaseViewHolder helper, final PackBean item) {
        helper.setText(R.id.tvSeq, String.valueOf(getData().size()-helper.getAdapterPosition()));
        helper.setText(R.id.tvLot, item.getLOTID());
        helper.setText(R.id.tvName, item.getPARTNAME());
        helper.setText(R.id.tvNum, String.valueOf(item.getMAINQTY()));
        CheckBox checkbox = helper.getView(R.id.checkbox);
        checkbox.setTag(helper.getAdapterPosition());
        checkbox.setChecked(mCheckStates.get(helper.getAdapterPosition(), false));
        checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = ((CheckBox) v).isChecked();
                item.setSelect(isChecked);
                int pos = (int) v.getTag();
                if (isChecked) {
                    mCheckStates.put(pos, true);
                } else {
                    mCheckStates.put(pos, false);
                }
            }
        });
    }
}
